package com.hexiang.config;

import java.util.*;

public class Example {
	public static void main(String[] args) {
		int[] nums={-1,0,1,2,-1,-4};
		System.out.println(threeSumv2(nums,0));
	}

	public static List<List<Integer>> threeSumv2(int[] nums, int target) {
		Arrays.sort(nums);
		Set<List<Integer>> resultList = new HashSet<>();
		//大循环
		for (int i = 0; i < nums.length; i++) {
			int d = target - nums[i];
			// j和k双指针循环定位，j在左端，k在右端
			for (int j=i+1,k=nums.length-1; j<nums.length; j++) {
				// k指针向左移动
				while (j<k && (nums[j]+nums[k])>d) {
					k--;
				}
				//双指针重合，跳出本次循环
				if (j == k) {
					break;
				}
				if (nums[j] + nums[k] == d) {
					List<Integer> list = Arrays.asList(nums[i], nums[j], nums[k]);
					resultList.add(list);
				}
			}
		}
		return new ArrayList<>(resultList);
	}
	}